/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client.activity;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import nu.zoom.android.gdata.project.client.Intents;
import nu.zoom.android.gdata.project.client.R;
import nu.zoom.android.gdata.project.client.StandardMenuHandler;
import nu.zoom.android.gdata.project.client.UIUtilities;
import nu.zoom.android.gdata.project.client.WKC;
import nu.zoom.android.gdata.project.integration.ProjectHostingClient;
import nu.zoom.android.gdata.project.integration.ProjectHostingClient.Comment;
import nu.zoom.android.gdata.project.integration.ProjectHostingClient.CommentFeed;
import nu.zoom.android.gdata.project.integration.ProjectHostingFactory;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CommentsList extends RetainedListActivity<ProjectHostingClient.CommentFeed, ProjectHostingClient.Comment> {
	private static final String LOG_TAG = WKC.LOG_TAG;

	public CommentsList() {
		super(R.layout.commentlist, R.id.commentpaginator);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Intent intent = getIntent();
		String title = intent.getStringExtra(Intents.Extra.ISSUE_NAME);
		if (title != null) {
			setTitle(title);
		}
	}

	@Override
	protected Dialog onCreateDialog(final int id) {
		return StandardMenuHandler.onCreateDialog(this, id);
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		StandardMenuHandler.onCreateOptionsMenu(this, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		return StandardMenuHandler.onOptionsItemSelected(this, item);
	}

	static class ViewHolder {
		TextView commentHeader;
		TextView commentContent;
	}

	@Override
	protected ArrayAdapter<Comment> getAdapter() {
		return new CommentListAdapter(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * nu.zoom.android.gdata.project.client.activity.FeedrunnerListActivity#
	 * runInBackground(android.net.Uri,
	 * nu.zoom.android.gdata.project.client.activity
	 * .FeedrunnerListActivity.FeedParam)
	 */
	@Override
	protected CommentFeed runInBackground(Uri uri,
			nu.zoom.android.gdata.project.client.activity.FeedrunnerListActivity.FeedParam parameter)
			throws IOException {
		final CommentFeed comments =
				ProjectHostingFactory.getClient().getComments(uri, startPosition, parameter.batchSize);
		return comments;
	}

	class CommentListAdapter extends ArrayAdapter<Comment> {

		/**
		 * @param context
		 * @param textViewResourceId
		 */
		public CommentListAdapter(Context context) {
			super(context, -1);
		}

		@Override
		public View getView(final int position, final View convertView, final ViewGroup parent) {
			View rowView = convertView;
			final ViewHolder holder;
			if (rowView == null) {
				rowView = getLayoutInflater().inflate(R.layout.commentlistrow, parent, false);
				holder = new ViewHolder();
				holder.commentHeader = (TextView) rowView.findViewById(R.id.CommentHeader);
				holder.commentContent = (TextView) rowView.findViewById(R.id.CommentContent);
				rowView.setTag(holder);
			} else {
				holder = (ViewHolder) rowView.getTag();
			}

			final Comment comment = getItem(position);

			final StringBuilder header = new StringBuilder(comment.title);
			if (comment.published != null) {
				String commentString = comment.published.trim();
				commentString = commentString.replaceAll("Z", "\\+0000$");
				final SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
				final DateFormat localizedDateFormat =
						DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
				try {
					final Date parse = dateParser.parse(commentString);
					header.append(", ").append(localizedDateFormat.format(parse));
				} catch (ParseException e) {
					Log.e(LOG_TAG, "Unable to parse date string: " + commentString, e);
				}
			}
			holder.commentHeader.setText(header);

			String commentData =
					((comment.content == null) ? getText(R.string.comment_contentempty).toString() : comment.content);
			holder.commentContent.setText(UIUtilities.plainifyMarkup(commentData));
			return rowView;
		}
	}
}
